﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace DrawManage.Tools
{
    public class zTree
    {
       
        public static string RebuildTree()
        {
            string JsonStr = "";
            string sql = "";
            BaseDao dao = new BaseDao();

            List<Dictionary<string, object>> MainList = new List<Dictionary<string, object>>();
            sql = "select NodeName as name,null as children, * from Node";
            MainList = dao.GetList(sql, new string[] { "name", "children", "Id", "NodeName", "ParentId", "DrawNum", "IsParent" });
            //foreach (var item in MainList)
            //{
            //    item.Add("children", null);
            //    item.Add("name", item["NodeName"].ToString() + " " + item["DrawNum"].ToString());
            //}
            var TreeRoot = MainList.FindAll(t => t["ParentId"].ToString() == "0");
            TreeRoot.Sort((x, y) => int.Parse(x["DrawNum"].ToString()).CompareTo(int.Parse(y["DrawNum"].ToString())));

            //搜索第一级
            foreach (var item in TreeRoot)
            {
                //搜索当前级下的
                item["children"] = GetChildren(MainList, item);
            }
            JavaScriptSerializer jss = new JavaScriptSerializer();
            JsonStr = jss.Serialize(TreeRoot);
            return JsonStr;

        }

        private static List<Dictionary<string, object>> GetChildren(List<Dictionary<string, object>> AllTree, Dictionary<string, object> Parent)
        {
            List<Dictionary<string, object>> ChildrenList = new List<Dictionary<string, object>>();

            string ParentId = Parent["Id"].ToString();
            var Nodes = AllTree.FindAll(t => t["ParentId"].ToString() == ParentId);
            //排序
            if (Nodes.Count > 1)
            {
                Nodes.Sort((x, y) => int.Parse(x["DrawNum"].ToString()).CompareTo(int.Parse(y["DrawNum"].ToString())));
            }
            ChildrenList = Nodes;
            //检测子树
            foreach (var item in Nodes)
            {
                item["children"] = GetChildren(AllTree, item);
            }

            if (ChildrenList.Count == 0)
            {
                return null;
            }
            else
            {
                return ChildrenList;
            }
        }



        public static string RebuildTree_DrawCombo(string ComboId)
        {
            string JsonStr = "";
            string sql = "";
            BaseDao dao = new BaseDao();

            List<Dictionary<string, object>> MainList = new List<Dictionary<string, object>>();
            sql = "select SerialDrawName as name,null as children, * from t_d_DrawCombo where IsDelete=0 and ComboId='" + ComboId + "'";
            MainList = dao.GetList(sql, new string[] { "name", "children", "Id", "ParentId", "SerialDrawName", "InstallNum","ComboId","MotorId","IncreaseNum","SerialDrawNum" });
            //foreach (var item in MainList)
            //{
            //    item.Add("children", null);
            //    item.Add("name", item["NodeName"].ToString() + " " + item["DrawNum"].ToString());
            //}
            var TreeRoot = MainList.FindAll(t => t["ParentId"].ToString() == "0");
            TreeRoot.Sort((x, y) => int.Parse(x["InstallNum"].ToString()).CompareTo(int.Parse(y["InstallNum"].ToString())));

            //搜索第一级
            foreach (var item in TreeRoot)
            {
                //搜索当前级下的
                item["children"] = GetChildren_DrawCombo(MainList, item);
            }
            JavaScriptSerializer jss = new JavaScriptSerializer();
            JsonStr = jss.Serialize(TreeRoot);
            return JsonStr;
        }


        private static List<Dictionary<string, object>> GetChildren_DrawCombo(List<Dictionary<string, object>> AllTree, Dictionary<string, object> Parent)
        {
            List<Dictionary<string, object>> ChildrenList = new List<Dictionary<string, object>>();

            string ParentId = Parent["Id"].ToString();
            var Nodes = AllTree.FindAll(t => t["ParentId"].ToString() == ParentId);
            //排序
            if (Nodes.Count > 1)
            {
                Nodes.Sort((x, y) => int.Parse(x["InstallNum"].ToString()).CompareTo(int.Parse(y["InstallNum"].ToString())));
            }
            ChildrenList = Nodes;
            //检测子树
            foreach (var item in Nodes)
            {
                item["children"] = GetChildren_DrawCombo(AllTree, item);
            }

            if (ChildrenList.Count == 0)
            {
                return null;
            }
            else
            {
                return ChildrenList;
            }
        }


    }
}